package com.yimixian.app.cart;

import com.squareup.picasso.Picasso;
import com.yimixian.app.EventBusDomain.CartItemRemoveMessage;
import com.yimixian.app.R;
import com.yimixian.app.SystemFramework;
import com.yimixian.app.adapter.CartItemAdapter;
import com.yimixian.app.common.Constants;
import com.yimixian.app.data.DataManager;
import com.yimixian.app.model.GoodsItem;
import com.yimixian.app.ui.PlusMinusIcon;
import com.yimixian.app.ui.wheelview.BaseFragmentFrameLayout;

import de.greenrobot.event.EventBus;

import android.app.Activity;
import android.content.Context;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * 自定义购物车ListView中Item的View Created by songzhiyang on 2015/8/19.
 */
public class CartNewGoodsItemView extends BaseFragmentFrameLayout {

    @InjectView(R.id.name_text)
    TextView mNameText;
    @InjectView(R.id.price_text)
    TextView mPriceText;
    @InjectView(R.id.plus_minus_icon_container)
    LinearLayout mPlucMinusIconContainer;
    @InjectView(R.id.iv_good_img)
    ImageView mGoodImage;
    @InjectView(R.id.unit_text)
    TextView mUnitText;
    @InjectView(R.id.text_tips)
    TextView mTextTips;
    @InjectView(R.id.fl_masking)
    FrameLayout mFlMasking;// 当物品数量为0时候的蒙版
    // @InjectView(R.id.divider_view) View mDividerView;
    @InjectView(R.id.item_remove)
    ImageView mItemRemove;

    protected GoodsItem mGoodsItem;
    protected int mQuantity;
    protected Context mContext;
    private CartItemAdapter mCartItemAdapter;

    private PlusMinusIcon mPlusMinusIcon;

    public CartNewGoodsItemView(Context context, CartItemAdapter cartItemAdapter) {
        super(context);
        this.mContext = context;
        this.mCartItemAdapter = cartItemAdapter;
        initCartGoodsItemView();
    }

    public CartNewGoodsItemView(Context context, AttributeSet attrs, CartItemAdapter cartItemAdapter) {
        super(context, attrs);
        this.mContext = context;
        this.mCartItemAdapter = cartItemAdapter;
        initCartGoodsItemView();
    }

    public CartNewGoodsItemView(Context context, AttributeSet attrs, int defStyle,
            CartItemAdapter cartItemAdapter) {
        super(context, attrs, defStyle);
        this.mContext = context;
        this.mCartItemAdapter = cartItemAdapter;
        initCartGoodsItemView();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
//        EventBus.getDefault().unregister(this);
    }

    /**
     * 初始化view 给 + / - button添加点击事件
     */
    protected void initCartGoodsItemView() {
        init();
        mQuantity = 0;
//        EventBus.getDefault().register(this);
    }

    protected void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.cart_goods_item_view_new, this);
        ButterKnife.inject(this);
    }

    /**
     * 给组合控件中的view填充数据
     *
     * @param goodsItem 购买的商品
     */
    public void bind(final GoodsItem goodsItem) {
        mGoodsItem = goodsItem;
        if (mGoodsItem == null) {
            return;
        }

        if(mPlusMinusIcon == null) {
            mPlusMinusIcon = new PlusMinusIcon(SystemFramework.getInstance().getGlobalContext(), goodsItem, Constants.STATE_CARTFRAGMENT_BUTTON_BROADCAST);
            mPlucMinusIconContainer.addView(mPlusMinusIcon);
        } else {
            mPlusMinusIcon.updateGoodsItem(goodsItem);
            if(goodsItem != null) {
                mPlusMinusIcon.setQuantity(Integer.parseInt(goodsItem.count));
            } else {
                mPlusMinusIcon.setQuantity(0);
            }
        }
        if(mPlusMinusIcon.getQuantity() == 0) {
            showFLMasking();
        } else {
            hideFLMasking();
        }

        mItemRemove.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                CartItemRemoveMessage message = new CartItemRemoveMessage();
                message.goodsId = goodsItem.id;
                EventBus.getDefault().post(message);
            }
        });
        if (mContext != null && mContext instanceof CartDetailActivity && ((CartDetailActivity)mContext).mIsEditState) {
            mItemRemove.setVisibility(View.VISIBLE);
        } else {
            mItemRemove.setVisibility(View.GONE);
        }
        mNameText.setText(goodsItem.name);
        mUnitText.setText(goodsItem.unitDesc);
        mPriceText.setText("¥" + goodsItem.unitPrice);
        Display display = ((Activity) getContext()).getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int screenWidth = size.x;
        Picasso.with(mContext)
                .load(DataManager.getInstance().getImageUrl(goodsItem.img, screenWidth / 2))
                .placeholder(R.drawable.placeholder).into(mGoodImage);
    }

    public void setShowDividerBelow(boolean value) {
        // mDividerView.setVisibility(value?View.VISIBLE: View.INVISIBLE);
    }

    public void showFLMasking() {
        if(mFlMasking != null) {
            mFlMasking.setVisibility(View.VISIBLE);
        }
        if(mPlusMinusIcon != null) {
            mPlusMinusIcon.setButtonCanClick(false);
        }
    }

    public void hideFLMasking() {
        if(mFlMasking != null) {
            mFlMasking.setVisibility(View.GONE);
        }
        if(mPlusMinusIcon != null) {
            mPlusMinusIcon.setButtonCanClick(true);
        }
    }

    public void setTextTips(String tips) {
        if(mTextTips != null) {
            mTextTips.setText(tips);
        }
    }

    public void hideTextTips() {
        if(mTextTips != null) {
            mTextTips.setVisibility(View.GONE);
        }
    }

    public void showTextTips() {
        if(mTextTips != null) {
            mTextTips.setVisibility(View.VISIBLE);
        }
    }

//    public void onEventMainThread(String message) {
//        if(message != null) {
//            if ((Constants.EVENT_BUS_CART_FRAGMENT_SHOW_FLMASKING+mGoodsItem.id).equals(message)) {
//                showFLMasking();
//            } else if ((Constants.EVENT_BUS_CART_FRAGMENT_HIDE_FLMASKING+mGoodsItem.id).equals(message)) {
//                hideFLMasking();
//            } else if ((Constants.EVENT_BUS_CART_FRAGMENT_FREEGOODS_SHOW_LFMASKING+mGoodsItem.id).equals(message)) {
//                if(mPlusMinusIcon != null) {
//                    mPlusMinusIcon.setQuantityText(0);
//                    showFLMasking();
//                }
//            }
//        }
//    }
}
